Check if every request has a correct Content-Type.

Akinori MUSHA 10 年之前
父节点
当前提交
d60dfa7977
共有 1 个文件被更改,包括 15 次插入2 次删除
  1. 15 2
      spec/models/agents/post_agent_spec.rb

+ 15 - 2
spec/models/agents/post_agent_spec.rb

@@ -37,9 +37,13 @@ describe Agents::PostAgent do
37 37
         data = request.uri.query
38 38
       else
39 39
         if data = request.body
40
-          case request.headers['Content-Type']
41
-          when /json/
40
+          case request.headers['Content-Type'][/\A[^;\s]+/]
41
+          when 'application/x-www-form-urlencoded'
42
+            # ok
43
+          when 'application/json'
42 44
             data = ActiveSupport::JSON.decode(data)
45
+          else
46
+            raise "unexpected Content-Type: #{content_type}"
43 47
           end
44 48
         end
45 49
       end
@@ -113,6 +117,15 @@ describe Agents::PostAgent do
113 117
       @sent_requests[:post][0].should == @checker.options['payload'].to_query
114 118
     end
115 119
 
120
+    it "sends options['payload'] as JSON as a POST request" do
121
+      @checker.options['content_type'] = 'json'
122
+      lambda {
123
+        @checker.check
124
+      }.should change { @sent_requests[:post].length }.by(1)
125
+
126
+      @sent_requests[:post][0].should == @checker.options['payload']
127
+    end
128
+
116 129
     it "sends options['payload'] as a GET request" do
117 130
       @checker.options['method'] = 'get'
118 131
       lambda {